﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    //单例模式
    //对象静态私有化
    private static GameManager _instance;
    //属性公有化
    public static GameManager instance { get; set; }

    public Transform canvas;
    public int xColumn=0;//列
    public int yRow=0;//行
    public GameObject gridPrefab;//格子
    //甜品种类枚举类型
    public enum SweetType {
        Empty,Normal,Barrier,RowClear,CollumnClear,RainbowCandy,Count//count为标记类型
    }
    //甜品预制体字典,通过甜品种类得到相应的gameobject
    public Dictionary<SweetType, GameObject> sweetPrefabDict=new Dictionary<SweetType, GameObject>();
    //结构体
    [System.Serializable]
    public struct SweetPrefab {
        public SweetType type;
        public GameObject prefab;
    }
    
    public SweetPrefab[] sweetPrefabs;//通过给结构体数组赋值来填充字典
    public GameSweet[,] sweets;//实例化的甜品的脚本对应的二维数组
    void Awake() {
        instance = this;
    }
    // Start is called before the first frame update
    void Start() {
        sweets = new GameSweet[xColumn, yRow];
        //给字典赋值
        foreach (var sp in sweetPrefabs) {
            //不包含则添加至字典
            if (!sweetPrefabDict.ContainsKey(sp.type)) {
                sweetPrefabDict.Add(sp.type, sp.prefab);
            }
        }
        //按照自顶下下,由左到右的顺序实例化格子,并为格子填充甜品预制体
        for (int j = yRow - 1; j >= 0; j--) {
            for (int i = 0; i < xColumn; i++) {
                GameObject chocolate = Instantiate(gridPrefab, canvas);//生成甜品的背景格子
                //设置甜品格子的位置
                chocolate.GetComponent<RectTransform>().anchoredPosition = new Vector2(-490 + 1080 / 10 * i, -910 + 1080 / 10 * j);
                //将甜品的预制体挂在脚本放到预制体二维数组中,生成为格子的子物体
                GameObject tempGO = Instantiate(sweetPrefabDict[SweetType.Normal], chocolate.transform);
                sweets[j,i] = tempGO.GetComponent<GameSweet>();
                //将生成的甜品锚点位置设置为相对父物体的零向量
                tempGO.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
                //yield return new WaitForSeconds(0.1f);
                //初始化sweet的数据[坐标,类型等]
                sweets[j,i].Init(-490 + 1080 / 10 * i, -910 + 1080 / 10 * j,this,SweetType.Normal);
                if (sweets[j, i].CanColor()) {
                    sweets[j, i].ColorSweetComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[j, i].GetComponent<ColorSweet>().NumberColors));
                    //Debug.Log("cancolor");
                }
               
            }
        }

        for (int j = yRow - 1; j >= 0; j--) {
            for (int i = 0; i < xColumn; i++) {
                sweets[j,i] = CreateNewSweet(j, i, SweetType.Empty);
            }
        }
    }

    // Update is called once per frame
    void Update() {

    }
    //产生甜品
    public GameSweet CreateNewSweet(int x,int y,SweetType type) {
       GameObject newSweet=Instantiate(sweetPrefabDict[type],Vector2.zero,Quaternion.identity);
       Destroy(sweets[x, y].transform.gameObject);
       newSweet.transform.parent = sweets[x, y].transform.parent;
       sweets[x, y] = newSweet.GetComponent<GameSweet>();
       return sweets[x, y];
    }
}
